<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 同事圈控制器
 * @package app\index\controller
 */
class Moments extends Home
{
    public $noNeedLogin = [];

    public function lists()
    {
        if ($this->request->isGet()) {
            $getData = $this->request->get();
            if (empty($getData['limit'])) {
                $getData['limit'] = 10;
            }
            $userInfo = $this->userInfo;
            $data = Db::name('qt_moments')
                ->alias('m')
                ->leftJoin('qt_users u', 'm.user_id = u.id')
                ->field('m.*,u.nickname,u.avatar')
                ->where('m.team_id', $userInfo['team_id'])
                ->order('m.id desc')
                ->paginate($getData['limit'])->each(function ($item) {
                    //处理头像
                    if (empty($item['avatar'])) {
                        $item['avatar'] = config('site_url') . '/static/home/img/zcool.jpg';
                    }else{
                        $item['avatar'] = get_img_path($item['avatar']);
                    }
                    //处理图片
                    if(!empty($item['image'])){
                        $imageRes = explode(',',$item['image']);
                        foreach ($imageRes as $key=>$value){
                            $imageRes[$key] = get_img_path($value);
                        }
                        $item['image'] = $imageRes;
                    }
                    //是否喜欢
                    $userInfo = $this->userInfo;
                    $like = Db::name('qt_moments_log')->where(['monents_id'=>$item['id'],'user_id'=>$userInfo['id'],'type'=>'2'])->count();
                    if($like > 0){
                        $item['is_like'] = 1;
                    }else{
                        $item['is_like'] = 0;
                    }
                    return $item;
                });
            $this->result('请求成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 同事圈详情
     */
    public function detail(){
        if ($this->request->isGet()) {
            $getData = $this->request->get();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $userInfo = $this->userInfo;
            $data = Db::name('qt_moments')
                ->alias('m')
                ->leftJoin('qt_users u', 'm.user_id = u.id')
                ->field('m.*,u.nickname,u.avatar')
                ->where('m.id', $getData['id'])
                ->find();
            //处理头像
            if (empty($data['avatar'])) {
                $data['avatar'] = config('site_url') . '/static/home/img/zcool.jpg';
            }else{
                $data['avatar'] = get_img_path($data['avatar']);
            }
            //处理图片
            if(!empty($data['image'])){
                $imageRes = explode(',',$data['image']);
                foreach ($imageRes as $key=>$value){
                    $imageRes[$key] = get_img_path($value);
                }
                $data['image'] = $imageRes;
            }
            $like = Db::name('qt_moments_log')->where(['monents_id'=>$getData['id'],'user_id'=>$userInfo['id'],'type'=>'2'])->count();
            if($like > 0){
                $data['is_like'] = 1;
            }else{
                $data['is_like'] = 0;
            }
            //评论列表
            $cmommentRes = Db::name('qt_moments_log')
                ->field('ml.content,u.nickname')
                ->alias('ml')
                ->leftJoin('qt_users u','ml.user_id = u.id')
                ->where('ml.monents_id',$getData['id'])
                ->where(['ml.monents_id'=>$getData['id'],'ml.type'=>1])
                ->select();
            $data['comments'] = $cmommentRes;
            $this->result('请求成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 添加评论
     */
    public function add_comment(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['content']) || !isset($getData['content'])){
                $this->result('缺少参数','',204);
            }
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $insertData['user_id'] = $userInfo['id'];
            $insertData['group_id'] = $userInfo['group_id'];
            $insertData['content'] = $getData['content'];
            $insertData['monents_id'] = $getData['id'];
            $insertData['type'] = 1;
            $insertData['created_time'] = time();
            if(Db::name('qt_moments_log')->insert($insertData)){
                $this->result('评论成功','',200);
            }
            $this->result('评论失败','',204);
        }
    }

    /**
     * 发布同事圈
     */
    public function add(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['content']) || !isset($getData['content'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $insertData['user_id'] = $userInfo['id'];
            $insertData['team_id'] = $userInfo['team_id'];
            $insertData['content'] = $getData['content'];
            $insertData['image'] = $getData['image'];
            $insertData['created_time'] = time();
            if(Db::name('qt_moments')->insert($insertData)){
                $this->result('发布成功','',200);
            }
            $this->result('发布失败','',204);
        }
    }

    /**
     * 点赞
     */
    public function like(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $find = Db::name('qt_moments_log')->where(['user_id'=>$userInfo['id'],'group_id'=>$userInfo['group_id'],'monents_id'=>$getData['id'],'type'=>'2'])->find();
            if(empty($find)){
                $insertData['user_id'] = $userInfo['id'];
                $insertData['group_id'] = $userInfo['group_id'];
                $insertData['monents_id'] = $getData['id'];
                $insertData['type'] = 2;
                $insertData['created_time'] = time();
                Db::name('qt_moments')->where('id',$getData['id'])->setInc('like_num');
                if(Db::name('qt_moments_log')->insert($insertData)){
                    $this->result('点赞成功','',200);
                }
                $this->result('点赞失败','',204);
            }
            $this->result('你已经点过赞了','',200);
        }
    }

    /**
     * 取消点赞
     */
    public function del_like(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $find = Db::name('qt_moments_log')->where(['user_id'=>$userInfo['id'],'group_id'=>$userInfo['group_id'],'monents_id'=>$getData['id'],'type'=>'2'])->find();
            if(empty($find)){
                $this->result('没找到你的点赞','',204);
            }
            $del = Db::name('qt_moments_log')->where('id',$find['id'])->delete();
            Db::name('qt_moments')->where('id',$getData['id'])->setDec('like_num');
            if($del){
                $this->result('取消成功','',200);
            }
            $this->result('取消失败','',204);
        }
    }

}
